package net.danlew.android.joda.sample.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.global.icu.text.DateFormat;

import static ohos.agp.components.Text.TruncationMode.AUTO_SCROLLING;
import static ohos.agp.utils.LayoutAlignment.CENTER;
import static ohos.agp.utils.LayoutAlignment.HORIZONTAL_CENTER;
import static ohos.global.icu.text.DateFormat.YEAR;

public class MainAbilitySlice extends AbilitySlice {

    public static String FORMAT_SHOW_TIME = ohos.global.icu.text.DateFormat.HOUR_MINUTE;
    public static String FORMAT_SHOW_WEEKDAY = ohos.global.icu.text.DateFormat.WEEKDAY;
    public static String FORMAT_SHOW_YEAR = YEAR;
    public static String FORMAT_SHOW_DATE = DateFormat.DAY;
    public static String FORMAT_NO_MONTH_DAY = DateFormat.YEAR_MONTH;
    public static String FORMAT_ABBREV_WEEKDAY = DateFormat.ABBR_WEEKDAY;
    public static String FORMAT_ABBREV_MONTH = DateFormat.ABBR_MONTH;
    public static String FORMAT_NUMERIC_DATE = DateFormat.NUM_MONTH_WEEKDAY_DAY;
    public static String FORMAT_ABBREV_ALL = DateFormat.ABBR_MONTH_DAY; //略有不同
    private ComponentContainer mContent;
    private ScrollView scrollView = new ScrollView(this);
    private  ScrollView.LayoutConfig layoutConfig = new StackLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);
    private DirectionalLayout directionalLayout = new DirectionalLayout(this);
    private DirectionalLayout.LayoutConfig layoutConfigD = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        directionalLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        directionalLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
        directionalLayout.setOrientation(Component.VERTICAL);
        directionalLayout.setPadding(32, 32, 80, 80);

        ShapeElement element = new ShapeElement();
        element.setShape(0);
        element.setRgbColor(new RgbColor(255, 255, 255));
        directionalLayout.setBackground(element);
        layoutConfigD.setMargins(400,5,0,5);

        Text welcome = new Text(this);
//            layoutConfig.gravity = LayoutAlignment.CENTER_HORIZONTAL;
        welcome.setLayoutConfig(layoutConfigD);
        welcome.setText("JodaTime测试主页面");
        welcome.setTextColor(Color.RED);
        welcome.setTextAlignment(HORIZONTAL_CENTER);
        welcome.setTextSize(60);
        directionalLayout.addComponent(welcome);


        Button button1 = new Button(this);
//        directionalLayout.gravity = LayoutAlignment.CENTER_HORIZONTAL;
        layoutConfigD.alignment = 70;
        button1.setLayoutConfig(layoutConfigD);

        button1.setText("进行sampleDateTime测试");
        button1.setTextSize(40);

        button1.setTruncationMode(AUTO_SCROLLING);
        button1.setAutoScrollingDuration(6000);
        button1.setAutoScrollingCount(100);
        button1.startAutoScrolling();
        button1.setId(123);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbPalette.CYAN);
        background.setCornerRadius(25);
        button1.setBackground(background);
        button1.setPadding(10, 10, 10, 10);
        button1.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                AbilitySlice targetSlice = new sampleDateTimeslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button1);


        Button button2 = new Button(this);

        button2.setLayoutConfig(layoutConfigD);
        button2.setText("进行sampleLocalDate测试");
        button2.setTextSize(40);

        button2.setTruncationMode(AUTO_SCROLLING);
        button2.setAutoScrollingDuration(6000);
        button2.setAutoScrollingCount(100);
        button2.startAutoScrolling();
        button2.setId(123);

        background.setRgbColor(RgbPalette.CYAN);
        background.setCornerRadius(25);
        button2.setBackground(background);
        button2.setPadding(10, 10, 10, 10);
        button2.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleLocalDateslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button2);


        Button button3 = new Button(this);
        button3.setLayoutConfig(layoutConfigD);
        button3.setText("进行sampleFormatDateTime测试");
        button3.setTextSize(40);
        button3.setBottom(3);

        button3.setTruncationMode(AUTO_SCROLLING);
        button3.setAutoScrollingDuration(6000);
        button3.setAutoScrollingCount(100);
        button3.startAutoScrolling();
        button3.setId(123);

        background.setRgbColor(RgbPalette.CYAN);
        background.setCornerRadius(25);
        button3.setBackground(background);
        button3.setPadding(10, 10, 10, 10);
        button3.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleFormatDateTimeslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button3);


        Button button4 = new Button(this);

        button4.setLayoutConfig(layoutConfigD);
        button4.setText("进行sampleDateRange测试");
        button4.setTextSize(40);
        button3.setBottom(3);

        button4.setTruncationMode(AUTO_SCROLLING);
        button4.setAutoScrollingDuration(6000);
        button4.setAutoScrollingCount(100);
        button4.startAutoScrolling();
        button4.setId(123);

        background.setRgbColor(new RgbColor(0xFF51A8DD));
        background.setCornerRadius(25);
        button4.setBackground(background);
        button4.setPadding(10, 10, 10, 10);
        button4.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleDateRangeslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button4);


        Button button5 = new Button(this);

        button5.setLayoutConfig(layoutConfigD);
        button5.setText("进行sampleFormatDuration测试");
        button5.setTextSize(40);

        button5.setTruncationMode(AUTO_SCROLLING);
        button5.setAutoScrollingDuration(6000);
        button5.setAutoScrollingCount(100);
        button5.startAutoScrolling();
        button5.setId(123);

        background.setRgbColor(new RgbColor(0xFF51A8DD));
        background.setCornerRadius(25);
        button5.setBackground(background);
        button5.setPadding(10, 10, 10, 10);
        button5.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleFormatDurationslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button5);


        Button button6 = new Button(this);

        button6.setLayoutConfig(layoutConfigD);
        button6.setText("进行sampleIsToday测试");
        button6.setTextSize(40);

        button6.setTruncationMode(AUTO_SCROLLING);
        button6.setAutoScrollingDuration(6000);
        button6.setAutoScrollingCount(100);
        button6.startAutoScrolling();
        button6.setId(123);

        background.setRgbColor(new RgbColor(0xFF51A8DD));
        background.setCornerRadius(25);
        button6.setBackground(background);
        button6.setPadding(10, 10, 10, 10);
        button6.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleIsTodayslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button6);


        Button button7 = new Button(this);

        button7.setLayoutConfig(layoutConfigD);
        button7.setText("进行sampleGetRelativeTimeSpanString测试");
        button7.setTextSize(40);

        button7.setTruncationMode(AUTO_SCROLLING);
        button7.setAutoScrollingDuration(6000);
        button7.setAutoScrollingCount(100);
        button7.startAutoScrolling();
        button7.setId(123);

        background.setRgbColor(new RgbColor(0xFF51A8DD));
        background.setCornerRadius(25);
        button7.setBackground(background);
        button7.setPadding(10, 10, 10, 10);
        button7.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleGetRelativeTimeSpanStringslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button7);


        Button button8 = new Button(this);
        button8.setLayoutConfig(layoutConfigD);
        button8.setText("进行sampleGetRelativeTimeSpanStringWithPreposition测试");
        button8.setTextSize(40);
//        button8.setAutoFontSize(true,true);

        button8.setTruncationMode(AUTO_SCROLLING);
        button8.setAutoScrollingDuration(6000);
        button8.setAutoScrollingCount(100);
        button8.startAutoScrolling();
        button8.setId(123);

        background.setRgbColor(new RgbColor(0xFF51A8DD));
        background.setCornerRadius(25);
        button8.setBackground(background);
        button8.setPadding(10, 10, 10, 10);
        button8.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleGetRelativeTimeSpanStringWithPrepositionslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button8);


        Button button9 = new Button(this);

        button9.setLayoutConfig(layoutConfigD);
        button9.setText("进行sampleGetRelativeDateTimeString测试");
        button9.setTextSize(40);

        button9.setTruncationMode(AUTO_SCROLLING);
        button9.setAutoScrollingDuration(6000);
        button9.setAutoScrollingCount(100);
        button9.startAutoScrolling();
        button9.setId(123);

        background.setRgbColor(new RgbColor(0xFF51A8DD));
        background.setCornerRadius(25);
        button9.setBackground(background);
        button9.setPadding(10, 10, 10, 10);
        button9.setClickedListener(new Component.ClickedListener() {
            @Override
            // 在控件中增加对点击事件的监听
            public void onClick(Component component) {
                // 按钮被点击
                // 按钮被按下后，需要执行的操作
                AbilitySlice targetSlice = new sampleGetRelativeDateTimeStringslice() ;
                Intent intent = new Intent();
                present(targetSlice, intent);
//
//        AbilitySlice intSlice = new IntTest();
//        Intent sliceIntent = new Intent();
//        present(intSlice, sliceIntent);
            }
        });
        directionalLayout.addComponent(button9);




        scrollView.addComponent(directionalLayout);

        super.setUIContent(scrollView);


//        sampleDateTime();
//        sampleLocalDate();
//        sampleFormatDateTime();
//        sampleDateRange();
//
//            try {
//                sampleFormatDuration();   ok
//            } catch (IOException e) {
//                e.printStackTrace();
//            } catch (NotExistException e) {
//                e.printStackTrace();
//            } catch (WrongTypeException e) {
//                e.printStackTrace();
//            }
//            sampleIsToday();
//            try {
//                sampleGetRelativeTimeSpanString();
//            } catch (NotExistException | IOException | WrongTypeException e) {
//                e.printStackTrace();
//            }
////            try {
////                sampleGetRelativeTimeSpanStringWithPreposition();
////            } catch (NotExistException | IOException | WrongTypeException e) {
////                e.printStackTrace();
////            }
//            try {
//                sampleGetRelativeDateTimeString();
//            } catch (NotExistException | WrongTypeException | IOException e) {
//                e.printStackTrace();
//            }
    }

//    private void sampleDateTime() {
//        List<String> text = new ArrayList<String>();
//        DateTime now = DateTime.now();
//        text.add("Now: " + now);
//        text.add("Now + 30 minutes: " + now.plusMinutes(30));
//        text.add("Now + 5 hours: " + now.plusHours(5));
//        text.add("Now + 2 days: " + now.plusDays(2)+"\n\n");
//        addSample("DateTime", text);
//    }

//    private void sampleLocalDate() {
//        List<String> text = new ArrayList<String>();
//        LocalDate now = LocalDate.now();
//        text.add("Now: " + now);
//        text.add("Now + 2 days: " + now.plusDays(2));
//        text.add("Now + 3 months: " + now.plusMonths(3));
//        addSample("LocalDate", text);
//    }

    // You can mix/match most flags for the desired output format
//    private void sampleFormatDateTime() {
//        List<String> text = new ArrayList<String>();
//        DateTime now = DateTime.now();
//        text.add("Show time: " + DateUtils.formatDateTime(this, now, FORMAT_SHOW_TIME));
//        text.add("Show date: " + DateUtils.formatDateTime(this, now, FORMAT_SHOW_DATE));
//        text.add("Numeric date: " + DateUtils.formatDateTime(this, now, FORMAT_NUMERIC_DATE));
//        text.add("Show date (abbreviated): " + DateUtils.formatDateTime(this, now, FORMAT_ABBREV_ALL));
////        text.add("Show date and time: " + DateUtils.formatDateTime(this, now, FORMAT_SHOW_DATE
////            | DateUtils.FORMAT_SHOW_TIME));
////        text.add("Show date (force year): " + DateUtils.formatDateTime(this, now, DateUtils.FORMAT_SHOW_DATE
////            | DateUtils.FORMAT_SHOW_YEAR));
//        addSample("DateUtils.formatDateTime()", text);
//    }

    // You can mix/match most flags for the desired output format
//    private void sampleDateRange() {
//        List<String> text = new ArrayList<String>();
//        DateTime start = DateTime.now();
//        DateTime end = start.plusMinutes(30).plusHours(2).plusDays(56);
//       text.add("Range: " + DateUtils.formatDateRange(this, start, end, YEAR_MONTH_DAY));
//        text.add("Range (with year): " + DateUtils.formatDateRange(this, start, end, YEAR));
//        text.add("Range (abbreviated): " + DateUtils.formatDateRange(this, start, end, ABBR_GENERIC_TZ));
//        text.add("Range (with time): " + DateUtils.formatDateRange(this, start, end, 	HOUR24_MINUTE));
//        addSample("DateUtils.formatDateRange()", text);
//    }

//    private void sampleFormatElapsedTime() {
//        List<String> text = new ArrayList<String>();
//        text.add("25 seconds: " + DateUtils.formatElapsedTime(Duration.standardSeconds(25)));
//        text.add("3 minutes: " + DateUtils.formatElapsedTime(Duration.standardMinutes(3)));
//        text.add("3 minutes, 25 seconds: " + DateUtils.formatElapsedTime(
//            Duration.standardMinutes(3).plus(Duration.standardSeconds(25))));
//        text.add("3 hours: " + DateUtils.formatElapsedTime(Duration.standardHours(3)));
//        text.add("3 hours, 3 minutes: " + DateUtils.formatElapsedTime(
//            Duration.standardHours(3).plus(Duration.standardMinutes(3))));
//        addSample("DateUtils.formatElapsedTime()", text);
//    }

//    private void sampleFormatDuration() throws IOException, NotExistException, WrongTypeException {
//        List<String> text = new ArrayList<String>();
//        text.add("Seconds: " + DateUtils.formatDuration(this, Duration.standardSeconds(25)));
//        text.add("Minutes: " + DateUtils.formatDuration(this, Duration.standardMinutes(5)));
//        text.add("Hours: " + DateUtils.formatDuration(this, Duration.standardHours(3)));
//        addSample("DateUtils.formatDuration()", text);
//    }
//
//    private void sampleIsToday() {
//        List<String> text = new ArrayList<String>();
//        LocalDate today = LocalDate.now();
//        text.add("Today: " + DateUtils.isToday(today));
//        text.add("Tomorrow: " + DateUtils.isToday(today.plusDays(1)));
//        text.add("Yesterday: " + DateUtils.isToday(today.minusDays(1)));
//        addSample("DateUtils.isToday()", text);
//    }

//    private void sampleGetRelativeTimeSpanString() throws NotExistException, WrongTypeException, IOException {
//        List<String> text = new ArrayList<String>();
//        DateTime now = DateTime.now();
//        text.add("Short future: " + DateUtils.getRelativeTimeSpanString(this, now.plusMinutes(25)));
//        text.add("Medium future: " + DateUtils.getRelativeTimeSpanString(this, now.plusHours(5)));
//        text.add("Long future: " + DateUtils.getRelativeTimeSpanString(this, now.plusDays(3)));
//        text.add("Short past: " + DateUtils.getRelativeTimeSpanString(this, now.minusMinutes(25)));
//        text.add("Medium past: " + DateUtils.getRelativeTimeSpanString(this, now.minusHours(5)));
//        text.add("Long past: " + DateUtils.getRelativeTimeSpanString(this, now.minusDays(3)));
//        addSample("DateUtils.getRelativeTimeSpanString()", text);
//    }
//
//    private void sampleGetRelativeTimeSpanStringWithPreposition() throws NotExistException, WrongTypeException, IOException {
//        List<String> text = new ArrayList<String>();
//        DateTime now = DateTime.now();
//        text.add("Short future: " + DateUtils.getRelativeTimeSpanString(this, now.plusMinutes(25), true));
//        text.add("Medium future: " + DateUtils.getRelativeTimeSpanString(this, now.plusHours(5), true));
//        text.add("Long future: " + DateUtils.getRelativeTimeSpanString(this, now.plusDays(3), true));
//        text.add("Short past: " + DateUtils.getRelativeTimeSpanString(this, now.minusMinutes(25), true));
//        text.add("Medium past: " + DateUtils.getRelativeTimeSpanString(this, now.minusHours(5), true));
//        text.add("Long past: " + DateUtils.getRelativeTimeSpanString(this, now.minusDays(3), true));
//        addSample("DateUtils.getRelativeTimeSpanString() (with preposition)", text);
//    }

//    private void sampleGetRelativeDateTimeString() throws NotExistException, WrongTypeException, IOException {
//        List<String> text = new ArrayList<String>();
//        DateTime now = DateTime.now();
//        text.add("Short future: " + DateUtils.getRelativeDateTimeString(this, now.plusMinutes(25), null, 0));
//        text.add("Medium future: " + DateUtils.getRelativeDateTimeString(this, now.plusHours(5), null, 0));
//        text.add("Long future: " + DateUtils.getRelativeDateTimeString(this, now.plusDays(3), null, 0));
//        text.add("Short past: " + DateUtils.getRelativeDateTimeString(this, now.minusMinutes(25), null, 0));
//        text.add("Medium past: " + DateUtils.getRelativeDateTimeString(this, now.minusHours(5), null, 0));
//        text.add("Long past: " + DateUtils.getRelativeDateTimeString(this, now.minusDays(3), null, 0));
//        addSample("DateUtils.getRelativeDateTimeString()", text);
//}
//
//    private void addSample(CharSequence title, Iterable<String> text) {
//        addSample(title, join("\n",text));
//    }
//    public static String join( CharSequence delimiter,  Iterable tokens) {
//        final Iterator<?> it = tokens.iterator();
//        if (!it.hasNext()) {
//            return "";
//        }
//        final StringBuilder sb = new StringBuilder();
//        sb.append(it.next());
//        while (it.hasNext()) {
//            sb.append(delimiter);
//            sb.append(it.next());
//        }
//        return sb.toString();
//    }
//
//    private void addSample(CharSequence title, CharSequence text) {
//
////        ((Text) view.findViewById(R.id.title)).setText(title);
////        ((Text) view.findViewById(R.id.text)).setText(text);
//        Text titleText = new Text(this);
////            layoutConfig.gravity = LayoutAlignment.CENTER_HORIZONTAL;
//        titleText.setLayoutConfig(layoutConfigD);
//        titleText.setText((String) title);
//        titleText.setTextSize(70);
//        titleText.setMultipleLine(true);
//        Text textT = new Text(this);
////            layoutConfig.gravity = LayoutAlignment.CENTER_HORIZONTAL;
//        textT.setLayoutConfig(layoutConfigD);
//        textT.setText((String) text);
//        textT.setTextSize(70);
//        textT.setMultipleLine(true);
//        directionalLayout.addComponent(titleText);
//        directionalLayout.addComponent(textT);
//    }

}
